/**
 * 2017年5月3日
 */
package util.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import org.lab.libsvm.svm_node;

import pojo.dto.services.DocumentVector;

/**
 * @author Alex
 *
 */
public class LibSVMReader {
	final File src;
	Vector<svm_node[]> vx;
	Vector<Double> vy;
	public LibSVMReader(File src) {
		this.src = src;
	}
	public /*Iterator<DocumentVector>*/void read() throws FileNotFoundException, IOException{
		try(BufferedReader fp = new BufferedReader(new FileReader(this.src))){
			vy = new Vector<Double>();
			vx = new Vector<svm_node[]>();
			int max_index = 0;
			while(true)
			{
				String line = fp.readLine();
				if(line == null) break;
				StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");

				vy.addElement(Double.parseDouble(st.nextToken()));
				int m = st.countTokens()/2;
				svm_node[] x = new svm_node[m];
				for(int j=0;j<m;j++)
				{
					x[j] = new svm_node();
					x[j].index = Integer.parseInt((st.nextToken()));
					x[j].value = Double.parseDouble(st.nextToken());
				}
				if(m>0) max_index = Math.max(max_index, x[m-1].index);
				vx.addElement(x);
			}
		}
	}
	/**
	 * @return the vx
	 */
	public Vector<svm_node[]> getVx() {
		return vx;
	}
	/**
	 * @param vx the vx to set
	 */
	public void setVx(Vector<svm_node[]> vx) {
		this.vx = vx;
	}
	/**
	 * @return the vy
	 */
	public Vector<Double> getVy() {
		return vy;
	}
	/**
	 * @param vy the vy to set
	 */
	public void setVy(Vector<Double> vy) {
		this.vy = vy;
	}
	
	
	
	
}
